package com.isscollage.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.isscollage.entity.Passenger;
import com.isscollage.entity.Users;
import com.isscollage.search.PassengerSearch;
import com.isscollage.service.LoginService;
import com.isscollage.service.UserService;
import com.isscollage.utils.UUIDUtils;

@Controller
@RequestMapping("users")
public class UserController {
	
	@Autowired
	private UserService userService;
	@Autowired
	private LoginService loginService;

	@RequestMapping("toConcat")
	public String toConcat( Map<String, Object> map, PassengerSearch search)/*@RequestParam(value = "pn", defaultValue="1") Integer pn,*/ {
		if(search==null) {
			search = new PassengerSearch();
		}
//使用PageHelper.startPage()方法进行分页参数传递
//		PageHelper.startPage(pn, 5);
		List<Passenger> list = userService.searchByCon(search);
		PageInfo<Passenger> page = new PageInfo<Passenger>(list,3);
		map.put("page", page);
		map.put("search",search);
		UUIDUtils.getUUID();
		return "passenger/passengerList";
	}
	
	@RequestMapping("getPassengerById")
	@ResponseBody
	public Passenger getPassengerById(Integer passengerId){
		Passenger passenger=null;
		if(passengerId!=null){
			passenger=userService.getPassengerById(passengerId);
			System.out.println(passenger.getPassengerId());
		}			
		return passenger;
	}
	
	@RequestMapping("update")
	@ResponseBody
	public boolean update(Passenger passenger){
		boolean flag = false;
		if(passenger!=null && passenger.getPassengerId()!=null){
			flag = userService.update(passenger);	
		}
		return flag;
	}
	
	@RequestMapping("insert")
	@ResponseBody
	public boolean insert(Passenger passenger){
		boolean flag=userService.insert(passenger);
		return flag;
	}
	
	@RequestMapping("deleteAll")
	@ResponseBody
	public boolean deleteAll(@RequestBody Integer[] ids){
		boolean flag=userService.deleteAll(ids);
		return flag;
	}
	
	@RequestMapping("checkAccountNo")
	@ResponseBody
	public boolean checkAccountNo(HttpSession session,Double totalPrice){
		Users user = (Users) session.getAttribute("user");
		String userId=user.getUserId();
		Users newUser = userService.getUserByUserId(userId);
		if(newUser!=null && totalPrice!=null){
			if(newUser.getAccountNo()-totalPrice>0){
				return true;
			}
		}
		return false;
	}
	
	//用户基本信息操作
	
	
	@RequestMapping("showUser")
	public String showUser(Model model,HttpSession session){
		Users user=(Users) session.getAttribute("user");
		String userId=user.getUserId();
		Users newUser=userService.getUserByUserId(userId);
		model.addAttribute("user", newUser);
		return "user/user";
	}
	
	@RequestMapping("userPassword")
	public String userPassword(HttpSession session){		
		return "user/userPassword";
	}
	
	@RequestMapping("editPassword")
	@ResponseBody
	public Integer editPassword(HttpSession session,String password,String newPassword){
		Integer result=null;
		Users user=(Users) session.getAttribute("user");
		if(user!=null && user.getUserId()!=null && !"".equals(user.getUserId())){
			Users u=loginService.login(user.getUserId(),password);
			if(u!=null && u.getUserId()!=null){
				u.setPassword(newPassword);
				boolean flag=userService.updateUser(u);
				if(flag){
					result=1;
				}else{
					result=0;
				}
			}else{
				result=0;
			}
		}
		return result;
	}
	
	@RequestMapping("toChangeTel")
	public String toChangeTel(){
		return "user/userTel";
	}
	
	@RequestMapping("editTel")
	@ResponseBody
	public Integer editTel(HttpSession session,String tel,String newTel){
		Integer result=null;
		Users user=(Users) session.getAttribute("user");
		if(user!=null && user.getUserId()!=null && !"".equals(user.getUserId())){
			Users u=loginService.loginTel(user.getUserId(),tel);
			if(u!=null && u.getUserId()!=null){
				u.setTel(newTel);
				boolean flag=userService.updateUser(u);
				if(flag){
					result=1;
				}else{
					result=0;
				}
			}else{
				result=0;
			}
		}
		return result;
	}
	
	@RequestMapping("toAccount")
	public String toAccount(){
		return "user/userAccount";
	}
	
	@RequestMapping("editAccountNo")
	@ResponseBody
	public Integer editAccountNo(Model model,HttpSession session,double newAccountNo){
		Integer result=null;
		Users user=(Users) session.getAttribute("user");
		model.addAttribute("user", user);		
		if(user!=null){
			double d=user.getAccountNo()+newAccountNo;
			user.setAccountNo(d);
			boolean flag=userService.updateUser(user);
			if(flag){
				result=1;
			}else{
				result=0;
			}
			}else{
				result=0;
			}
		return result;
	}
}
